﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using WarehouseManager.Model;
using WarehouseManager.Core;
using System.Data;
using System.Data.SqlClient;
namespace WarehouseManager.DataAccess
{
    class NhaCCAcess
    {
        dbConnection db = new dbConnection();
        public DataTable LayNCC()
        {
            return db.executeSelectProcedureQueryNonParameter("select_ncc");
        }
        //---- Tìm kiếm theo mã ncc
        public DataView timkiemmancc(string mancc)
        {
            DataView dataView = LayNCC().DefaultView;
            dataView.RowFilter = string.Format("maNCC like '%{0}%'", mancc);
            return dataView;
        }
        //---- Tìm kiếm theo tên ncc
        public DataView timkiemtenncc(string Tenncc)
        {
            DataView dataView = LayNCC().DefaultView;
            dataView.RowFilter = string.Format("tenNCC like '%{0}%'", Tenncc);
            return dataView;
        }
        public DataTable timncctheoma(string mancc)
        {
            SqlParameter[] listParam = new SqlParameter[1];
            SqlParameter ma = new SqlParameter("@maNCC", SqlDbType.Char, 10);
            ma.Value = mancc;
            listParam[0] = ma;
            return db.executeSelectProcedureQuery("tim_ncc", listParam);
        }
        //---- Thêm ncc
        public int themncc(NhaCC ncc)
        {

            if (timncctheoma(ncc.MaNCC).Rows.Count > 0) return 0;
            else
            {
                SqlParameter[] listParam = new SqlParameter[5];
                SqlParameter mancc= new SqlParameter("@maNCC", SqlDbType.VarChar, 10);
                mancc.Value =ncc.MaNCC;
                listParam[0] = mancc;
                listParam[1] = new SqlParameter("@tenNCC",ncc.TenNCC); 
                listParam[2] = new SqlParameter("@diaChi",ncc.DiaChi);
                listParam[3] = new SqlParameter("@soDT", ncc.SoDT);
                listParam[4] = new SqlParameter("@email",ncc.Email);
                db.executeInsertProcedureQuery("insert_ncc", listParam);
                return 1;
            }
        }
        //---- Sửa ncc 
        public int suancc(NhaCC ncc)
        {
            if (timkiemmancc(ncc.MaNCC)==null)
                return 0;
            else
            {
               SqlParameter[] listParam = new SqlParameter[5];
                SqlParameter mancc= new SqlParameter("@maNCC", SqlDbType.VarChar, 10);
                mancc.Value =ncc.MaNCC;
                listParam[0] = mancc;
                listParam[1] = new SqlParameter("@tenNCC",ncc.TenNCC); 
                listParam[2] = new SqlParameter("@diaChi",ncc.DiaChi);
                listParam[3] = new SqlParameter("@soDT", ncc.SoDT);
                listParam[4] = new SqlParameter("@email",ncc.Email);
                db.executeInsertProcedureQuery("update_ncc", listParam);
                return 1;

            }
        }
        // xóa ncc
        public int xoancc(NhaCC ncc)
        {
            if (timkiemmancc(ncc.MaNCC) == null)
                return 0;
            else
            {
                SqlParameter[] listParam = new SqlParameter[5];
                SqlParameter mancc = new SqlParameter("@maNCC", SqlDbType.VarChar, 10);
                mancc.Value = ncc.MaNCC;
                listParam[0] = mancc;
                listParam[1] = new SqlParameter("@tenNCC", ncc.TenNCC);
                listParam[2] = new SqlParameter("@diaChi", ncc.DiaChi);
                listParam[3] = new SqlParameter("@soDT", ncc.SoDT);
                listParam[4] = new SqlParameter("@email", ncc.Email);
                db.executeInsertProcedureQuery("delete_ncc", listParam);
                return 1;

            }

            }
    }
}
